//
//  HBHPostDetailCell.m
//  HubeiHotel
//
//  Created by 张露杰 on 18/4/2.
//  Copyright © 2018年 张露杰. All rights reserved.
//

#import "HBHPostDetailCell.h"
#import "CommemtView.h"

@implementation HBHPostDetailCell

{
    NSMutableArray *_imagVs;
    NSArray *_images;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

-(void)loadCellWithData:(NSDictionary *)data
{
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    _imagVs = [NSMutableArray new];
    
    UIImageView *headIcon = [TomamaUI createImageView:CGRectMake(15*screenW_6, 15*screenW_6, 30*screenW_6, 30*screenW_6) backGroundImageV:@"forumHeader"];
    [headIcon sd_setImageWithURL:[NSURL URLWithString:data[@"headPortrait"]] placeholderImage:[UIImage imageNamed:@"forumHeader"]];
    [self.contentView addSubview:headIcon];
    headIcon.layer.masksToBounds = YES;
    headIcon.layer.cornerRadius = 15*screenW_6;
    headIcon.clipsToBounds = YES;
    headIcon.contentMode = UIViewContentModeScaleAspectFill;
    
    UILabel *nameLabel = [TomamaUI createLabel:CGRectMake(CGRectGetMaxX(headIcon.frame) + 10*screenW_6, 0, [TomamaUI getStringWeithtWith:data[@"nickName"] andHeigth:15*screenW_6 andFont:[UIFont systemFontOfSize:15*screenW_6]], 15*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:15*screenW_6] textColor:TextBlack text:data[@"nickName"]];
    [self.contentView addSubview:nameLabel];
    nameLabel.center = CGPointMake(nameLabel.center.x, headIcon.center.y);

    if (![[Storage getToken] isEqualToString:data[@"userId"]]) {
        NSString *title = @"关注";
        if ([data[@"isFollow"] boolValue]) {
            title = @"已关注";
        }
        UIButton *btn = [TomamaUI createButton:0 Frame:CGRectMake(SCREENW - 68*screenW_6, 0, 53*screenW_6, 25*screenW_6) targ:self sel:@selector(follow) titleColor:[UIColor colorWithHexString:@"#d6d6d6"] font:[UIFont systemFontOfSize:13*screenW_6] title:title image:nil backGroundImage:nil];
        [self.contentView addSubview:btn];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 5*screenW_6;
        btn.layer.borderWidth = .5;
        btn.layer.borderColor = TextGray.CGColor;
        btn.center = CGPointMake(btn.center.x, nameLabel.center.y);
    }
    
    UILabel *contentLabel = [TomamaUI createLabel:CGRectMake(15*screenW_6, CGRectGetMaxY(headIcon.frame) + 10*screenW_6, SCREENW - 30*screenW_6, [TomamaUI getStringHeightWith:data[@"content"] andWeith:SCREENW -30*screenW_6 andFont:[UIFont systemFontOfSize:14*screenW_6]]) textAligment:0 font:[UIFont systemFontOfSize:14*screenW_6] textColor:TextBlack text:data[@"content"]];
    [self.contentView addSubview:contentLabel];
    contentLabel.numberOfLines = 0;
    
    _images = data[@"imgList"];
    for (int i = 0; i < _images.count; i++) {
        UIImageView *imageV = [TomamaUI createImageView:CGRectMake(i*((SCREENW - (_images.count - 1)*2*screenW_6)/_images.count + 2*screenW_6), CGRectGetMaxY(contentLabel.frame) + 15*screenW_6, (SCREENW - _images.count*2*screenW_6)/_images.count, 175*screenW_6) backGroundImageV:@""];
        [self.contentView addSubview:imageV];
        imageV.clipsToBounds = YES;
        imageV.contentMode = UIViewContentModeScaleAspectFill;
        [imageV sd_setImageWithURL:[NSURL URLWithString:_images[i][@"imgUrl"]] placeholderImage:nil];
        imageV.userInteractionEnabled = YES;
        [imageV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)]];
        [_imagVs addObject:imageV];
    }
    
    CommemtView *commemtView = [CommemtView new];
    commemtView.frame = CGRectMake(0, CGRectGetMaxY(contentLabel.frame) + 15*screenW_6, SCREENW, 40*screenW_6);
    if (_images.count) {
        commemtView.frame = CGRectMake(0, CGRectGetMaxY(contentLabel.frame) + 190*screenW_6, SCREENW, 40*screenW_6);
    }
    __weak typeof(self)weakSelf = self;
    commemtView.commentBlock = ^(NSInteger Taptag)
    {
        if (weakSelf.commentTapBlock) {
            weakSelf.commentTapBlock(Taptag);
        }
    };
    [self.contentView addSubview:commemtView];
    [commemtView CommetNumber:[NSString stringWithFormat:@"%@",data[@"commentNum"]] CollectNumber:[NSString stringWithFormat:@"%@",data[@"clickNum"]] IsComment:[data[@"isClick"] boolValue] IsCollect:[data[@"isClick"] boolValue]];

    UILabel *timeLabel = [TomamaUI createLabel:CGRectMake(15*screenW_6, 0, SCREENW - CGRectGetMaxX(nameLabel.frame) - 16*screenW_6, 13*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:13*screenW_6] textColor:TextGray text:@""];
    timeLabel.center = CGPointMake(timeLabel.center.x, commemtView.center.y);
    [self.contentView addSubview:timeLabel];
    timeLabel.text = [NSString stringWithFormat:@"发布于 %@",[RWBGetTime getTimeWithTimeString:[TomamaUI getTimeStrWithString:data[@"createDate"] geshi:@"YYYY-MM-dd HH:mm:ss"]]];
}

-(void)showImage:(UITapGestureRecognizer *)tap
{
    NSMutableArray *items = [NSMutableArray array];
    UIView *fromView = tap.view;
    for (int i =0; i < _images.count; i++) {
        YYPhotoGroupItem *item = [YYPhotoGroupItem new];
        item.thumbView = _imagVs[i];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",_images[i]]];
        item.largeImageURL = url;
        [items addObject:item];
    }
    YYPhotoBrowseView *groupView = [[YYPhotoBrowseView alloc]initWithGroupItems:items];
    [groupView presentFromImageView:fromView toContainer:[UIApplication sharedApplication].keyWindow
                           animated:YES completion:nil];
}

-(void)follow
{
    if (_folloBlock) {
        _folloBlock();
    }
}

@end
